package com.allenkerr.common;

import com.allenkerr.config.FishtripInternalRequestConfig;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;
//import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
//import org.apache.commons.codec.digest.DigestUtils;

/**
 * Created by jason on 2017/4/21.
 */
@Component
public class FishtripInternalSecurity {

    @Resource
    private FishtripInternalRequestConfig config;

    private String getRequestSecurityCode(String appId, String timestamp, String appKey) {
        System.out.println("appId = " + appId);
        System.out.println("timestamp = " + timestamp);
        System.out.println("appKey = " + appKey);
        /**
         * appId = review
         * timestamp = 1498365882
         * appKey = 5a4e0819aa41643e19fab9c5f71550936897da39b909f024951605f5fca5e498
         */
        String md5Hex1 = DigestUtils.md5Hex(appId + timestamp + appKey);
        String md5Hex2 = org.springframework.util.DigestUtils.md5DigestAsHex((appId + timestamp + appKey).getBytes());
        System.out.println("md5Hex1 = " + md5Hex1);
        System.out.println("md5Hex2 = " + md5Hex2);
        /**
         * md5Hex1 = 139ebb7a6cdbe72849e085dee6e40c0c
         * md5Hex2 = 139ebb7a6cdbe72849e085dee6e40c0c
         */
        return md5Hex2;
    }

    public Map getGlobeSecurity() {
        return this.getRequestSecurity(config.getGlobe());
    }

    private Map getRequestSecurity(String appKey) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        Map postPara = new TreeMap<String, Objects>();
        postPara.put("app_id", config.getId());
        postPara.put("timestamp", timestamp);
        postPara.put("sign", this.getRequestSecurityCode(config.getId(), timestamp, appKey));
        return postPara;
    }
}
